<?php
namespace app\common\lib;

//安装依赖包
//最低SDK版本要求：facebody-20191230的SDK版本需大于等于3.0.8
//可以在此仓库地址中引用最新版本SDK：https://packagist.org/packages/alibabacloud/facebody-20191230
//composer require alibabacloud/facebody-20191230

use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Exception\TeaError;
use app\exception\IndexException;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectFaceAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

class DetectFaceAdvance
{

    protected $accessKeyId;
    protected $accessSecret;

    public static $instance;
    public function __construct()
    {
        $this->accessKeyId = config('param.alibaba_cloud')['facebody']['accesskeyid'];
        $this->accessSecret = config('param.alibaba_cloud')['facebody']['accesssecret'];

        self::createClient($this->accessKeyId,$this->accessSecret);
    }

    public static function getInstance($config = [])
    {
//        if(self::$instance){
//            return self::$instance;
//        }

        return new self($config);
    }
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Facebody Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        //初始化配置对象Darabonba\OpenApi\Models\Config。
        //Config对象存放accessKeyId、accessKeySecret、endpoint等配置
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new Facebody($config);
    }


    public function getFaceBody($image)
    {
        //如果人脸识别开关，已关闭，则直接返回空数组
        if(config('param.facebody_open_close')==false){
            return [];
        }

        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html
        // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。

        try {
            $client = self::createClient($this->accessKeyId,$this->accessSecret);
            // 场景一，使用本地文件
            //$file = fopen(runtime_path('666.png'), 'rb');
            //$stream = new Stream($file);
            // 场景二，使用任意可访问的url
             $file = fopen($image, 'rb');
             $stream = new Stream($file);
            $detectFaceAdvanceRequest = new DetectFaceAdvanceRequest([
                "imageURLObject" => $stream,
                "landmark" => true,
                "quality" => true,
                "maxFaceNumber" => 5,
                "pose" => true
            ]);
            $runtime = new RuntimeOptions([]);
            $resp = $client->detectFaceAdvance($detectFaceAdvanceRequest, $runtime);

            # 获取整体结果
            //echo Utils::toJSONString($resp->body);
            return json_decode(Utils::toJSONString($resp->body), true);
        }catch (\Exception $exception) {
            throw new IndexException(4020,$exception->getMessage());
            # 获取整体报错信息
            //echo Utils::toJSONString($exception);
            # 获取单个字段
            //echo $exception->getCode();
            //return Utils::toJSONString($exception);
        }
    }
}